package com.ruoyi.web.controller.postResponse;

import java.util.List;

import com.ruoyi.conmentsComplain.service.ICommentsComplainService;
import com.ruoyi.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postResponse.domain.PostResponse;
import com.ruoyi.postResponse.service.IPostResponseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 帖子回复 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-10-15
 */
@Controller
@RequestMapping("/postResponse/postResponse")
public class PostResponseController extends BaseController
{
    private String prefix = "postResponse/postResponse";

	private String prefix2 = "conmentsComplain/commentsComplain";
	
	@Autowired
	private IPostResponseService postResponseService;

	@Autowired
	private ICommentsComplainService commentsComplainService;
	
	@RequiresPermissions("postResponse:postResponse:view")
	@GetMapping()
	public String postResponse()
	{
	    return prefix + "/postResponse";
	}
	
	/**
	 * 查询帖子回复列表
	 */
	@RequiresPermissions("postResponse:postResponse:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PostResponse postResponse)
	{
		startPage();
        List<PostResponse> list = postResponseService.selectPostResponseList(postResponse);
		return getDataTable(list);
	}

	/**
	 * 查询待审核帖子回复列表
	 */
	@RequiresPermissions("postResponse:postResponse:list")
	@PostMapping("/verifyList")
	@ResponseBody
	public TableDataInfo verifyList(PostResponse postResponse)
	{
		startPage();
		postResponse.setIsComplain(1);
		postResponse.setVerifyState(0);
		List<PostResponse> list = postResponseService.selectPostResponseList(postResponse);
		return getDataTable(list);
	}


	
	
	/**
	 * 导出帖子回复列表
	 */
	@RequiresPermissions("postResponse:postResponse:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PostResponse postResponse)
    {
    	List<PostResponse> list = postResponseService.selectPostResponseList(postResponse);
        ExcelUtil<PostResponse> util = new ExcelUtil<PostResponse>(PostResponse.class);
        return util.exportExcel(list, "postResponse");
    }
	
	/**
	 * 新增帖子回复
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存帖子回复
	 */
	@RequiresPermissions("postResponse:postResponse:add")
	@Log(title = "帖子回复", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PostResponse postResponse)
	{		
		return toAjax(postResponseService.insertPostResponse(postResponse));
	}

	/**
	 * 修改帖子回复
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		PostResponse postResponse = postResponseService.selectPostResponseById(id);
		mmap.put("postResponse", postResponse);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存帖子回复
	 */
	@RequiresPermissions("postResponse:postResponse:edit")
	@Log(title = "帖子回复", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PostResponse postResponse)
	{		
		return toAjax(postResponseService.updatePostResponse(postResponse));
	}
	
	/**
	 * 删除帖子回复
	 */
	@RequiresPermissions("postResponse:postResponse:remove")
	@Log(title = "帖子回复", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(postResponseService.deletePostResponseByIds(ids));
	}


	@PostMapping("/ignoreVerify")
	@ResponseBody
	public AjaxResult ignoreVerify(Integer id) throws Exception {

//		Post postPersist = postService.selectPostById(id);
//		postPersist.setIsComplain(2);
//		postPersist.setVerifyState("1");
//		postService.updatePost(postPersist);
//		return toAjax(postComplainService.updateComplainVerify(1,id));
		PostResponse postResponse = postResponseService.selectPostResponseById(id);
		postResponse.setIsComplain(2);
		postResponse.setVerifyState(1);
		postResponseService.updatePostResponse(postResponse);
		return toAjax(commentsComplainService.updateComplainVerify(1,id));
	}

	@PostMapping("/hideVerify")
	@ResponseBody
	public AjaxResult hideVerify(Integer id) throws Exception {

//		Post postPersist = postService.selectPostById(id);
//		postPersist.setIsComplain(2);
//		postPersist.setVerifyState("2");
//		postService.updatePost(postPersist);
//		return toAjax(postComplainService.updateComplainVerify(2,id));
		PostResponse postResponse = postResponseService.selectPostResponseById(id);
		postResponse.setIsComplain(2);
		postResponse.setVerifyState(2);
		postResponseService.updatePostResponse(postResponse);
		//隐藏评论回复
		postResponseService.updateResponseToComments(id);

		return toAjax(commentsComplainService.updateComplainVerify(2,id));
	}

	@GetMapping("/getComplain/{id}")
	public String getComplain(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("commentId", id);
		return prefix2 + "/commentsComplain";
	}
}
